

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/lou-jinming/img/fluid.png">
  <link rel="icon" href="/lou-jinming/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="娄金明">
  <meta name="keywords" content="">
  
    <meta name="description" content="class类123456789class Person &amp;#123;  age: number | undefined  gender &#x3D; &amp;#x27;男&amp;#x27;&amp;#125;const p1 &#x3D; new Person()console.log(p1.age); &#x2F;&#x2F; undefinedconsole.log(p1.gender); &#x2F;&#x2F; 男 class的构造函数1234567891011121">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript高级">
<meta property="og:url" content="http://example.com/lou-jinming/2022/08/30/TypeScript%E9%AB%98%E7%BA%A7/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="class类123456789class Person &amp;#123;  age: number | undefined  gender &#x3D; &amp;#x27;男&amp;#x27;&amp;#125;const p1 &#x3D; new Person()console.log(p1.age); &#x2F;&#x2F; undefinedconsole.log(p1.gender); &#x2F;&#x2F; 男 class的构造函数1234567891011121">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-08-30T06:00:51.000Z">
<meta property="article:modified_time" content="2022-08-30T06:01:19.801Z">
<meta property="article:author" content="娄金明">
<meta property="article:tag" content="TypeScript">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>TypeScript高级 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/lou-jinming/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/lou-jinming/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/lou-jinming/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/lou-jinming/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/lou-jinming/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/lou-jinming/js/utils.js" ></script>
  <script  src="/lou-jinming/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/lou-jinming/">
      <strong>凄美地</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/lou-jinming/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/lou-jinming/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/lou-jinming/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/lou-jinming/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/lou-jinming/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/lou-jinming/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="TypeScript高级"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-08-30 14:00" pubdate>
          2022年8月30日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          59 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">TypeScript高级</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="class类"><a href="#class类" class="headerlink" title="class类"></a>class类</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Person</span> &#123;<br>  <span class="hljs-attr">age</span>: <span class="hljs-built_in">number</span> | <span class="hljs-literal">undefined</span><br>  gender = <span class="hljs-string">&#x27;男&#x27;</span><br>&#125;<br><br><span class="hljs-keyword">const</span> p1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p1.<span class="hljs-property">age</span>); <span class="hljs-comment">// undefined</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p1.<span class="hljs-property">gender</span>); <span class="hljs-comment">// 男</span><br></code></pre></td></tr></table></figure>
<h2 id="class的构造函数"><a href="#class的构造函数" class="headerlink" title="class的构造函数"></a>class的构造函数</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Person1</span> &#123;<br>  <span class="hljs-attr">age</span>: <span class="hljs-built_in">number</span><br>  <span class="hljs-attr">gender</span>: <span class="hljs-built_in">string</span><br><br>  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">age: <span class="hljs-built_in">number</span>, gender: <span class="hljs-built_in">string</span></span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">gender</span> = gender<br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">let</span> p2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person1</span>(<span class="hljs-number">18</span>, <span class="hljs-string">&#x27;男&#x27;</span>)<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p2.<span class="hljs-property">age</span>); <span class="hljs-comment">// 18</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p2.<span class="hljs-property">gender</span>); <span class="hljs-comment">// 男</span><br></code></pre></td></tr></table></figure>
<h2 id="class实例方法"><a href="#class实例方法" class="headerlink" title="class实例方法"></a>class实例方法</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Point</span> &#123;<br>  x = <span class="hljs-number">1</span><br>  y = <span class="hljs-number">2</span><br><br>  <span class="hljs-title function_">scale</span>(<span class="hljs-params">n: <span class="hljs-built_in">number</span></span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">x</span> *= n <br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">y</span> *= n <br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">let</span> pp = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Point</span>()<br><br>pp.<span class="hljs-title function_">scale</span>(<span class="hljs-number">10</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pp.<span class="hljs-property">x</span>); <span class="hljs-comment">// 10</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pp.<span class="hljs-property">y</span>); <span class="hljs-comment">// 20</span><br><br></code></pre></td></tr></table></figure>
<h2 id="class继承（extends）继承父类"><a href="#class继承（extends）继承父类" class="headerlink" title="class继承（extends）继承父类"></a>class继承（extends）继承父类</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Animal</span> &#123;<br>  <span class="hljs-title function_">move</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;走两步&#x27;</span>)<br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Dog</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">Animal</span> &#123;<br>  <span class="hljs-title function_">bark</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;汪汪汪&#x27;</span>)<br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">const</span> d = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Dog</span><br>d.<span class="hljs-title function_">move</span>()<br>d.<span class="hljs-title function_">bark</span>()<br></code></pre></td></tr></table></figure>
<h2 id="class继承（implements）实现接口"><a href="#class继承（implements）实现接口" class="headerlink" title="class继承（implements）实现接口"></a>class继承（implements）实现接口</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Singale</span>&#123;<br>  <span class="hljs-title function_">sing</span>(): <span class="hljs-built_in">void</span><br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Person2</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Singale</span>&#123;<br>  <span class="hljs-title function_">sing</span>(): <span class="hljs-built_in">void</span> &#123;<br>      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;hahahaha&#x27;</span>)<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="class成员可见性"><a href="#class成员可见性" class="headerlink" title="class成员可见性"></a>class成员可见性</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// public (公有的) 默认</span><br><span class="hljs-comment">// protected (受保护的) 仅对其所在类和子类中（非实例对象）可见</span><br><span class="hljs-comment">// private (私有的) 只在类中可见，对实例对象以及子类也是不可见的</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Animal1</span> &#123;<br>  <span class="hljs-keyword">protected</span> <span class="hljs-title function_">move</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;走两步&#x27;</span>)<br>  &#125;<br><br>  <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">move</span>()<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;跑起来&#x27;</span>)<br>  &#125;<br><br>  <span class="hljs-keyword">private</span> <span class="hljs-title function_">__run__</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">move</span>()<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;跑起来&#x27;</span>)<br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Dog1</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">Animal1</span> &#123;<br>  <span class="hljs-title function_">bark</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">__run__</span>() <span class="hljs-comment">// 属性“__run__”为私有属性，只能在类“Animal1”中访问。(报错)</span><br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">move</span>() <span class="hljs-comment">// 不报错</span><br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;汪汪汪&#x27;</span>)<br>  &#125;<br>&#125;<br><br><span class="hljs-keyword">const</span> d1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Dog1</span><br>d1.<span class="hljs-title function_">move</span>() <span class="hljs-comment">// 属性“move”受保护，只能在类“Animal1”及其子类中访问。(报错)</span><br>d1.<span class="hljs-title function_">__run__</span>() <span class="hljs-comment">// 属性“__run__”为私有属性，只能在类“Animal1”中访问。(报错)</span><br>d1.<span class="hljs-title function_">bark</span>()<br></code></pre></td></tr></table></figure>
<h2 id="readonly只读修饰符"><a href="#readonly只读修饰符" class="headerlink" title="readonly只读修饰符"></a>readonly只读修饰符</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Person3</span> &#123;<br>  <span class="hljs-comment">// 只读属性</span><br>  <span class="hljs-comment">// 如果有默认值一定要手动指定值的类型 </span><br>  <span class="hljs-comment">// 否则根据类型推论推论出的类型，可能不是我们需要的类型</span><br>  <span class="hljs-comment">// 注意：只要是readonly修饰的属性，必须手动提供明确的类型</span><br>  <span class="hljs-keyword">readonly</span> <span class="hljs-attr">age</span>: <span class="hljs-built_in">number</span> = <span class="hljs-number">18</span><br><br>  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">age: <span class="hljs-built_in">number</span></span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>  &#125;<br><br>  <span class="hljs-comment">// readonly 不能修饰方法</span><br>  <span class="hljs-comment">// readonly setAge() &#123; // &quot;readonly&quot; 修饰符仅可出现在属性声明或索引签名中。 (报错)</span><br>  <span class="hljs-comment">//   this.age = 20 // 无法分配到 &quot;age&quot; ，因为它是只读属性。(报错)</span><br>  <span class="hljs-comment">// &#125;</span><br>&#125;<br><br><span class="hljs-comment">// 接口或者 &#123;&#125; 表示的对象类型，也可以使用readonly</span><br><br><span class="hljs-comment">// --</span><br><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">IPerson1</span>&#123;<br>  <span class="hljs-keyword">readonly</span> <span class="hljs-attr">name</span>: <span class="hljs-built_in">string</span><br>&#125;<br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">obj1</span>: <span class="hljs-title class_">IPerson1</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;jack&#x27;</span><br>&#125;<br><br><span class="hljs-comment">// obj1.name = &#x27;rose&#x27; // 无法分配到 &quot;name&quot; ，因为它是只读属性。(报错)</span><br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">obj2</span>: &#123;<span class="hljs-keyword">readonly</span> <span class="hljs-attr">name</span>: <span class="hljs-built_in">string</span>&#125; = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;jack&#x27;</span><br>&#125;<br><br><span class="hljs-comment">// obj2.name = &#x27;rose&#x27; // 报错</span><br></code></pre></td></tr></table></figure>
<h2 id="类型兼容性"><a href="#类型兼容性" class="headerlink" title="类型兼容性"></a>类型兼容性</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 演示类型兼容性</span><br><span class="hljs-keyword">let</span> arr3 = [<span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;b&#x27;</span>, <span class="hljs-string">&#x27;c&#x27;</span>]<br><br>arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> &#123;&#125;)<br>arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;&#125;)<br>arr.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">item, index, array</span>) =&gt;</span> &#123;&#125;)<br><br><span class="hljs-comment">// 结构化类型系统</span><br><span class="hljs-comment">// 两个类的兼容性演示</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Point1</span> &#123;<br>  x!: <span class="hljs-built_in">number</span><br>  y!: <span class="hljs-built_in">number</span><br>&#125;<br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Point2D</span> &#123;<br>  x!: <span class="hljs-built_in">number</span><br>  y!: <span class="hljs-built_in">number</span><br>  z!: <span class="hljs-built_in">number</span><br>&#125;<br><span class="hljs-comment">// 成员多的可以赋值给成员少的，两个相同的也可以</span><br><span class="hljs-keyword">const</span> <span class="hljs-attr">p4</span>: <span class="hljs-title class_">Point1</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Point2D</span><br></code></pre></td></tr></table></figure>
<h2 id="交叉类型"><a href="#交叉类型" class="headerlink" title="交叉类型"></a>交叉类型</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Person4</span> &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-built_in">string</span><br>  <span class="hljs-title function_">say</span>(): <span class="hljs-built_in">number</span><br>&#125;<br><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Contact</span> &#123;<br>  <span class="hljs-attr">phone</span>: <span class="hljs-built_in">string</span><br>&#125;<br><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">PersonDetail</span> = <span class="hljs-title class_">Person4</span> &amp; <span class="hljs-title class_">Contact</span><br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">obj3</span>: <span class="hljs-title class_">PersonDetail</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;jack&#x27;</span>,<br>  <span class="hljs-attr">phone</span>: <span class="hljs-string">&#x27;13323213123&#x27;</span>,<br>  <span class="hljs-title function_">say</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span><br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="交叉类型vs继承"><a href="#交叉类型vs继承" class="headerlink" title="交叉类型vs继承"></a>交叉类型vs继承</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 相同点：都可以实现对象类型的组合</span><br><span class="hljs-comment">// 不同点：两种方式实现类型组合时，对于同名属性之间，处理类型冲突的方式不同</span><br><span class="hljs-keyword">interface</span> A &#123;<br>  <span class="hljs-attr">fn</span>: <span class="hljs-function">(<span class="hljs-params">value: <span class="hljs-built_in">number</span></span>) =&gt;</span> <span class="hljs-built_in">string</span><br>&#125;<br><br><span class="hljs-comment">// 报错</span><br><span class="hljs-comment">// 不能将类型“(value: string) =&gt; string”分配给类型“(value: number) =&gt; string”。</span><br><span class="hljs-comment">// interface B extends A &#123;</span><br><span class="hljs-comment">//   fn: (value: string) =&gt; string</span><br><span class="hljs-comment">// &#125;</span><br><br><span class="hljs-keyword">interface</span> B &#123;<br>  <span class="hljs-attr">fn</span>: <span class="hljs-function">(<span class="hljs-params">value: <span class="hljs-built_in">string</span></span>) =&gt;</span> <span class="hljs-built_in">string</span><br>&#125;<br><br><span class="hljs-keyword">type</span> C = A &amp; B<br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">c</span>: C = &#123;<br>  <span class="hljs-title function_">fn</span>(<span class="hljs-params">value: <span class="hljs-built_in">number</span> | <span class="hljs-built_in">string</span></span>)&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;&#x27;</span><br>  &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h2 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 使用泛型来创建一个函数</span><br><span class="hljs-keyword">function</span> id&lt;<span class="hljs-title class_">Type</span>&gt;(<span class="hljs-attr">value</span>: <span class="hljs-title class_">Type</span>): <span class="hljs-title class_">Type</span> &#123;<br>  <span class="hljs-keyword">return</span> value<br>&#125;<br><br><span class="hljs-comment">// 调用泛型函数</span><br><br><span class="hljs-comment">// 1. 以 number 类型调用泛型函数</span><br><span class="hljs-keyword">const</span> num1 = id&lt;<span class="hljs-built_in">number</span>&gt;(<span class="hljs-number">10</span>)<br><span class="hljs-keyword">const</span> num2 = <span class="hljs-title function_">id</span>(<span class="hljs-number">10</span>) <span class="hljs-comment">// 简写</span><br><br><span class="hljs-comment">// 2. 以 string 类型调用泛型函数</span><br><span class="hljs-keyword">const</span> str1 = id&lt;<span class="hljs-built_in">string</span>&gt;(<span class="hljs-string">&#x27;a&#x27;</span>)<br><span class="hljs-keyword">const</span> str2 = <span class="hljs-title function_">id</span>(<span class="hljs-string">&#x27;a&#x27;</span>) <span class="hljs-comment">// 简写</span><br><br><span class="hljs-comment">// 通过泛型就做到了让 id 函数与多种不同的类型一起工作，实现了复用的同时保证了类型安全</span><br></code></pre></td></tr></table></figure>
<h2 id="泛型约束"><a href="#泛型约束" class="headerlink" title="泛型约束"></a>泛型约束</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 1.</span><br><span class="hljs-keyword">function</span> id1&lt;<span class="hljs-title class_">Type</span>&gt;(<span class="hljs-attr">value</span>: <span class="hljs-title class_">Type</span>[]): <span class="hljs-title class_">Type</span>[] &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value.<span class="hljs-property">length</span>)<br>  <span class="hljs-keyword">return</span> value<br>&#125;<br><br><span class="hljs-comment">// 2. extends 实现</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">ILength</span> &#123;<span class="hljs-attr">length</span>: <span class="hljs-built_in">number</span>&#125;<br><span class="hljs-keyword">function</span> id2&lt;<span class="hljs-title class_">Type</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ILength</span>&gt;(<span class="hljs-attr">value</span>: <span class="hljs-title class_">Type</span>): <span class="hljs-title class_">Type</span> &#123;<br>  value.<span class="hljs-property">length</span><br>  <span class="hljs-keyword">return</span> value<br>&#125;<br><br><span class="hljs-title function_">id2</span>([])<br><span class="hljs-title function_">id2</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])<br><br><span class="hljs-comment">// id2(123) // 类型“number”的参数不能赋给类型“ILength”的参数。报错</span><br><br><span class="hljs-comment">// 3. 多个泛型变量的情况</span><br><span class="hljs-comment">// keyof</span><br><span class="hljs-keyword">function</span> getProp&lt;<span class="hljs-title class_">Type</span>, <span class="hljs-title class_">Key</span> <span class="hljs-keyword">extends</span> keyof <span class="hljs-title class_">Type</span>&gt;(<span class="hljs-attr">obj</span>: <span class="hljs-title class_">Type</span>, <span class="hljs-attr">key</span>: <span class="hljs-title class_">Key</span>) &#123;<br>  <span class="hljs-keyword">return</span> obj[key]<br>&#125;<br><br><span class="hljs-title function_">getProp</span>(&#123;<span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;jack&#x27;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>&#125;, <span class="hljs-string">&#x27;age&#x27;</span>)<br><br><span class="hljs-comment">// 补充：了解</span><br><span class="hljs-comment">// 可以访问对象具有的方法</span><br><span class="hljs-title function_">getProp</span>(<span class="hljs-string">&#x27;abc&#x27;</span>, <span class="hljs-string">&#x27;split&#x27;</span>)<br><br><span class="hljs-comment">// 此处的 1 表示索引</span><br><span class="hljs-title function_">getProp</span>(<span class="hljs-string">&#x27;abc&#x27;</span>, <span class="hljs-number">1</span>) <br><span class="hljs-title function_">getProp</span>([<span class="hljs-number">1</span>], <span class="hljs-number">100</span>)<br></code></pre></td></tr></table></figure>
<h2 id="泛型接口"><a href="#泛型接口" class="headerlink" title="泛型接口"></a>泛型接口</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">interface</span> <span class="hljs-title class_">IdFun</span>&lt;<span class="hljs-title class_">Type</span>&gt; &#123;<br>  <span class="hljs-attr">id</span>: <span class="hljs-function">(<span class="hljs-params">value: Type</span>) =&gt;</span> <span class="hljs-title class_">Type</span><br>  <span class="hljs-attr">ids</span>: <span class="hljs-function">() =&gt;</span> <span class="hljs-title class_">Type</span>[]<br>&#125;<br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">obj4</span>: <span class="hljs-title class_">IdFun</span>&lt;<span class="hljs-built_in">number</span>&gt; = &#123;<br>  <span class="hljs-title function_">id</span>(<span class="hljs-params">value</span>) &#123;<br>    <span class="hljs-keyword">return</span> value<br>  &#125;,<br>  <span class="hljs-title function_">ids</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-keyword">return</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="泛型类"><a href="#泛型类" class="headerlink" title="泛型类"></a>泛型类</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 1.</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">GenericNumber</span>&lt;<span class="hljs-title class_">NumType</span>&gt; &#123;<br>  defaultValue!: <span class="hljs-title class_">NumType</span><br>  add!: <span class="hljs-function">(<span class="hljs-params">x: NumType, y: NumType</span>) =&gt;</span> <span class="hljs-title class_">NumType</span><br>&#125;<br><br><span class="hljs-comment">// 这种情况下明确指定类型</span><br><span class="hljs-keyword">const</span> myNum = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GenericNumber</span>&lt;<span class="hljs-built_in">number</span>&gt;()<br>myNum.<span class="hljs-property">defaultValue</span> = <span class="hljs-number">10</span><br><br><br><span class="hljs-comment">// 2.</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">GenericNumber1</span>&lt;<span class="hljs-title class_">NumType</span>&gt; &#123;<br>  defaultValue!: <span class="hljs-title class_">NumType</span><br>  add!: <span class="hljs-function">(<span class="hljs-params">x: NumType, y: NumType</span>) =&gt;</span> <span class="hljs-title class_">NumType</span><br><br>  <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value: NumType</span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">defaultValue</span> = value<br>  &#125;<br>&#125;<br><br><span class="hljs-comment">// 此时，可以省略 &lt;类型&gt; 不写</span><br><span class="hljs-keyword">const</span> myNum1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">GenericNumber1</span>(<span class="hljs-number">100</span>)<br>myNum1.<span class="hljs-property">defaultValue</span> = <span class="hljs-number">10</span><br><br><br></code></pre></td></tr></table></figure>
<h2 id="泛型工具类型"><a href="#泛型工具类型" class="headerlink" title="泛型工具类型"></a>泛型工具类型</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 1. Partial&lt;Type&gt; 用来构建（创建）一个类型，将Type的所有属性设置为可选</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Props</span> &#123;<br>  <span class="hljs-attr">id</span>: <span class="hljs-built_in">string</span><br>  <span class="hljs-attr">type</span>: <span class="hljs-built_in">string</span><br>  <span class="hljs-attr">children</span>: <span class="hljs-built_in">number</span>[]<br>&#125;<br><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">PartialProps</span> = <span class="hljs-title class_">Partial</span>&lt;<span class="hljs-title class_">Props</span>&gt;<br><span class="hljs-comment">// type PartialProps = &#123;</span><br><span class="hljs-comment">//   id?: string | undefined;</span><br><span class="hljs-comment">//   type?: string | undefined;</span><br><span class="hljs-comment">//   children?: number[] | undefined;</span><br><span class="hljs-comment">// &#125;</span><br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">pp1</span>: <span class="hljs-title class_">Props</span> = &#123;<br>  <span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;&#x27;</span>,<br>  <span class="hljs-attr">type</span>: <span class="hljs-string">&#x27;&#x27;</span>,<br>  <span class="hljs-attr">children</span>: [<span class="hljs-number">1</span>]<br>&#125;<br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">pp2</span>: <span class="hljs-title class_">PartialProps</span> = &#123;<br>  <span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;&#x27;</span><br>&#125;<br><br><span class="hljs-comment">// 2. Readonly&lt;Type&gt; 用来构建（创建）一个类型，将Type的所有属性设置为readonly(只读的)</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">ReadonlyProps</span> = <span class="hljs-title class_">Readonly</span>&lt;<span class="hljs-title class_">Props</span>&gt;<br><span class="hljs-comment">// type ReadonlyProps = &#123;</span><br><span class="hljs-comment">//   readonly id: string;</span><br><span class="hljs-comment">//   readonly type: string;</span><br><span class="hljs-comment">//   readonly children: number[];</span><br><span class="hljs-comment">// &#125;</span><br><br><span class="hljs-comment">// 3. Pick&lt;Type, Keys&gt; 从Type中选择一组属性来构造新类型 </span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">PinkProps</span> = <span class="hljs-title class_">Pick</span>&lt;<span class="hljs-title class_">Props</span>, <span class="hljs-string">&#x27;id&#x27;</span> | <span class="hljs-string">&#x27;type&#x27;</span>&gt;<br><span class="hljs-comment">// type PinkProps = &#123;</span><br><span class="hljs-comment">//   id: string;</span><br><span class="hljs-comment">//   type: string;</span><br><span class="hljs-comment">// &#125;</span><br><br><span class="hljs-comment">// 4. Record&lt;Keys, Type&gt; 构造一个对象类型，属性键为Keys，属性累死那个为Type</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">RecordObj</span> = <span class="hljs-title class_">Record</span>&lt;<span class="hljs-string">&#x27;a&#x27;</span> | <span class="hljs-string">&#x27;b&#x27;</span> | <span class="hljs-string">&#x27;c&#x27;</span>, <span class="hljs-built_in">string</span>[]&gt;<br><span class="hljs-comment">// type RecordObj = &#123;</span><br><span class="hljs-comment">//   a: string[];</span><br><span class="hljs-comment">//   b: string[];</span><br><span class="hljs-comment">//   c: string[];</span><br><span class="hljs-comment">// &#125;</span><br></code></pre></td></tr></table></figure>
<h2 id="索引签名类型"><a href="#索引签名类型" class="headerlink" title="索引签名类型"></a>索引签名类型</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 使用场景：当无法确定对象中有哪些属性（或者说对象中可以出现任意多个属性）</span><br><span class="hljs-comment">// 对象中，键的类型是string类型的</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">AnyObject</span> &#123;<br>  [<span class="hljs-attr">key</span>: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">number</span><br>&#125;<br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">objj</span>: <span class="hljs-title class_">AnyObject</span> = &#123;<br>  <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>,<br>  <span class="hljs-attr">abc</span>: <span class="hljs-number">22</span><br>&#125;<br><br><span class="hljs-comment">// 数组是特殊的对象，数组的键（索引）是数值类型</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">MyArray</span>&lt;<span class="hljs-title class_">Type</span>&gt; &#123;<br>  [<span class="hljs-attr">index</span>: <span class="hljs-built_in">number</span>]: <span class="hljs-title class_">Type</span><br>&#125;<br><br><span class="hljs-keyword">let</span> <span class="hljs-attr">arrr</span>: <span class="hljs-title class_">MyArray</span>&lt;<span class="hljs-built_in">number</span>&gt; = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>]<br></code></pre></td></tr></table></figure>
<h2 id="映射类型"><a href="#映射类型" class="headerlink" title="映射类型"></a>映射类型</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 基于旧类型创建新类型（对象类型），减少重复操作</span><br><span class="hljs-comment">// 传统写法：</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">PropKeys</span> = <span class="hljs-string">&#x27;x&#x27;</span> | <span class="hljs-string">&#x27;y&#x27;</span> | <span class="hljs-string">&#x27;z&#x27;</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Type1</span> = &#123;<span class="hljs-attr">x</span>: <span class="hljs-built_in">number</span>; <span class="hljs-attr">y</span>: <span class="hljs-built_in">number</span>; <span class="hljs-attr">z</span>: <span class="hljs-built_in">number</span>&#125;<br><br><span class="hljs-comment">// 映射类型写法</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Type2</span> = &#123;[key <span class="hljs-keyword">in</span> <span class="hljs-title class_">PropKeys</span>]: <span class="hljs-built_in">number</span>&#125;<br><br><span class="hljs-comment">// 不能在接口中使用</span><br><span class="hljs-comment">// interface type3 &#123;</span><br><span class="hljs-comment">//   [key in PropKeys]: number // 报错</span><br><span class="hljs-comment">// &#125;</span><br><br><span class="hljs-comment">// (keyof)</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Prop</span> = &#123;<span class="hljs-attr">a</span>: <span class="hljs-built_in">number</span>; <span class="hljs-attr">b</span>: <span class="hljs-built_in">string</span>; <span class="hljs-attr">c</span>: <span class="hljs-built_in">boolean</span>&#125;<br><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Type4</span> = &#123;[key <span class="hljs-keyword">in</span> keyof <span class="hljs-title class_">Prop</span>]: <span class="hljs-built_in">number</span>&#125; <br><span class="hljs-comment">// type Type4 = &#123;</span><br><span class="hljs-comment">//   a: number;</span><br><span class="hljs-comment">//   b: number;</span><br><span class="hljs-comment">//   c: number;</span><br><span class="hljs-comment">// &#125;</span><br></code></pre></td></tr></table></figure>
<h2 id="索引查询类型"><a href="#索引查询类型" class="headerlink" title="索引查询类型"></a>索引查询类型</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">type</span> <span class="hljs-title class_">Propss</span> = &#123;<span class="hljs-attr">a</span>: <span class="hljs-built_in">number</span>; <span class="hljs-attr">b</span>: <span class="hljs-built_in">string</span>; <span class="hljs-attr">c</span>: <span class="hljs-built_in">boolean</span>; <span class="hljs-attr">d</span>: <span class="hljs-built_in">number</span>&#125;<br><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">TypeA</span> = <span class="hljs-title class_">Propss</span>[<span class="hljs-string">&#x27;a&#x27;</span>] <span class="hljs-comment">// type TypeA = number</span><br><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">TypeB</span> = <span class="hljs-title class_">Propss</span>[<span class="hljs-string">&#x27;a&#x27;</span> | <span class="hljs-string">&#x27;b&#x27;</span>] <span class="hljs-comment">// type TypeB = string | number</span><br><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">TypeC</span> = <span class="hljs-title class_">Propss</span>[keyof <span class="hljs-title class_">Propss</span>] <span class="hljs-comment">// type TypeC = string | number | boolean</span><br></code></pre></td></tr></table></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/lou-jinming/tags/TypeScript/">#TypeScript</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>TypeScript高级</div>
      <div>http://example.com/lou-jinming/2022/08/30/TypeScript高级/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>娄金明</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年8月30日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/lou-jinming/2022/08/30/git%E5%88%86%E6%94%AF%E7%9A%84%E5%90%88%E5%B9%B6%E4%B8%8E%E6%8F%90%E4%BA%A4/" title="git分支的合并与提交">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">git分支的合并与提交</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/lou-jinming/2022/08/30/TypeScript%E5%9F%BA%E7%A1%80/" title="TypeScript基础">
                        <span class="hidden-mobile">TypeScript基础</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/lou-jinming/js/events.js" ></script>
<script  src="/lou-jinming/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/lou-jinming/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/lou-jinming/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/lou-jinming/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
